<?php
declare (strict_types = 1);

namespace app;

use think\App;
use think\Exception;
use think\exception\ValidateException;
use think\Validate;

/**
 * 控制器基础类
 */
abstract class BaseApiController extends BaseController
{

    /**
     * 结果监控
     * @param $rs
     * @param string $msg
     * @param string $errorMsg
     * @param int $code
     * @param array $data
     * @return \think\response\Json
     */
    protected function handleResponse($rs, $msg = '', $errorMsg = '', $code = 50000, $data = [])
    {
        if (is_bool($rs)) {
            return $rs ? json_success($msg, $data) : json_error($errorMsg, $code, $data);
        }
        return json_return($rs);
    }

    /**
     * 异常监控返回
     * @param \Exception $e
     * @return \think\response\Json
     */
    protected function handleException(\Exception $e)
    {
        if ($e instanceof Exception) {
            json_error($e->getMessage(), $e->getCode(), $e->getData());
        }
        return json_error($e->getMessage(), $e->getCode());
    }

}
